package com.chinaztt.fda.adapter;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chinaztt.fda.ui.R;
import com.chinaztt.fda.widget.photoview.EasePhotoView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 图片展示适配器
 * Created by zhengxizhen on 15/11/23.
 */
public class PhotoAdapter extends PagerAdapter {

    private List<String> mArrayData = new ArrayList<String>();

    private ImageLoader mImageLoader = ImageLoader.getInstance();

    private DisplayImageOptions mOptions = null;

    private boolean dataSetChanged;

    private ImageLoadingListener mAnimateFirstListener = new AnimateFirstDisplayListener();
    java.net.URLDecoder urlDecoder = new java.net.URLDecoder();

    public PhotoAdapter(List<String> dataList) {
        mArrayData = dataList;
        mOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true)
                .cacheOnDisc(true)
                        // .displayer(new RoundedBitmapDisplayer(20))
                .build();
    }

    public List<String> getmArrayData() {
        return mArrayData;
    }

    @Override
    public int getCount() {
        return mArrayData.size();
    }

    @Override
    public int getItemPosition(Object object) {
//        if (dataSetChanged) {
//            dataSetChanged = false;
        return POSITION_NONE;
//        }
//        return super.getItemPosition(object);
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        EasePhotoView photoView = new EasePhotoView(container.getContext());
        photoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        String photo = null;
        try {
            photo = urlDecoder.decode(mArrayData.get(position), "UTF-8");
            System.out.println("p:" + position + " \n" + photo);
            mImageLoader.displayImage(photo,photoView, mOptions, mAnimateFirstListener);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return photoView;
    }

    public Object getItem(int position) {
        if (position < mArrayData.size()) {
            return mArrayData.get(position);
        } else {
            return null;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

    @Override
    public void notifyDataSetChanged() {
        dataSetChanged = true;
        super.notifyDataSetChanged();
    }

}
